package xyz.zeer.endangeredanimal;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProtectAdapter extends RecyclerView.Adapter<ProtectAdapter.ViewHolder> {

    private static List<ProtectModel> yrs_protectModelList;
    private static Context yrs_context;
    private static ImageClicked yrs_imageClicked;

    public ProtectAdapter(List<ProtectModel> protectModelList, Context context, ImageClicked imageClicked) {
        this.yrs_protectModelList = protectModelList;
        this.yrs_context = context;
        this.yrs_imageClicked = imageClicked;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.yrs_protect_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int image = yrs_protectModelList.get(position).getImageResource();
        String name = yrs_protectModelList.get(position).getAnimalName();
        String info = yrs_protectModelList.get(position).getInfo();

        ViewHolder.setData(image, name, info);
    }

    @Override
    public int getItemCount() {
        return yrs_protectModelList.size();
    }

    public interface ImageClicked{
        void imageClicked(ProtectModel protectModel, Context context);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private static ImageView imageView;
        private static TextView textView;
        private static TextView textTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.AnimalImage);
            textView = itemView.findViewById(R.id.textViewProtect);
            textTitle = itemView.findViewById(R.id.textName);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yrs_imageClicked.imageClicked(yrs_protectModelList.get(getAdapterPosition()), yrs_context);
                }
            });

        }

        public static void setData(int image, String name, String info) {
            imageView.setImageResource(image);
            textView.setText(info);
            textTitle.setText(name);
        }
    }
}
